<template>
    <div class="message-card" @click="toMessagePage">
        <div class="avatar-container">
            <van-image
                class="avatar"
                round
                fit="cover"
                :src="message.avatarUrl || 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'"
                alt=""
            />
            <div v-if="!message.isRead" class="unread-badge"></div>
        </div>
        <div class="message-content">
            <div class="message-header">
                <div class="message-name">{{ message.userName }}</div>
                <div class="message-time">{{ formatTime(message.sendTime) }}</div>
            </div>
            <div class="message-text" :class="{ 'message-text-unread': !message.isRead }">
                {{ message.isCancel ? '[已撤回]' : message.content }}
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";

const router = useRouter();

interface MessageProps {
    message: {
        messageId: number;
        sendUserId: number;
        receiveUserId: number;
        receiveType: number;
        userName: string;
        avatarUrl: string;
        content: string;
        sendTime: string;
        isRead: number;
        isCancel: number;
    };
}

const props = defineProps<MessageProps>();

// 格式化时间为"今天 HH:MM"或"MM-DD HH:MM"格式
const formatTime = (timeStr: string): string => {
    if (!timeStr) return '';
    
    const date = new Date(timeStr);
    const now = new Date();
    
    // 判断是否是今天
    const isToday = date.getDate() === now.getDate() && 
                    date.getMonth() === now.getMonth() && 
                    date.getFullYear() === now.getFullYear();
    
    // 格式化小时和分钟
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const timeFormat = `${hours}:${minutes}`;
    
    if (isToday) {
        return `今天 ${timeFormat}`;
    } else {
        // 格式化月和日
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const day = date.getDate().toString().padStart(2, '0');
        return `${month}-${day} ${timeFormat}`;
    }
};

const toMessagePage = () => {
    // 导航到聊天详情页面
    router.push({
        name: "userChat",
        params: {
            toUserId: props.message.receiveType === 1 ? props.message.receiveUserId : props.message.sendUserId,
            receiveType: props.message.receiveType.toString(),
            status: "0"
        }
    });
};

</script>

<style scoped>
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

.message-card {
    display: flex;
    align-items: center;
    background-color: #fff;
    border-radius: 12px;
    margin: 8px 0;
    padding: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.avatar-container {
    position: relative;
    margin-right: 12px;
}

.avatar {
    width: 50px;
    height: 50px;
}

.unread-badge {
    position: absolute;
    top: 0;
    right: 0;
    width: 10px;
    height: 10px;
    background-color: #ee0a24;
    border-radius: 50%;
}

.message-content {
    flex: 1;
    overflow: hidden;
}

.message-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.message-name {
    font-size: 16px;
    font-weight: 500;
    color: #333;
}

.message-time {
    font-size: 12px;
    color: #999;
}

.message-text {
    font-size: 14px;
    color: #666;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    line-height: 1.5;
}

.message-text-unread {
    color: #333;
    font-weight: 500;
}
</style>
